var userGroupService = {
  getGroupList: function (options) {
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/Group/getGroupList',
      method: 'POST',
      data: util.signature({
        cityId: options.cityId,
        pageNumber: options.pageIndex,
        pageSize: options.pageSize,
        group_name: options.group_name ? options.group_name:""
      }),
      header: util.buildRequestheader(),
      success: function (res, statusCode) {
        if (res.data.Success) {
          options.success.call(that, res.data.ResultData);
        } else {
          util.processFailureRequest(options.failure, res, statusCode);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  isManager: function (options){
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/Group/isGroupManager',
      method: 'POST',
      data: util.signature({
        user_id: app.globalData.apiUser,
      }),
      header: util.buildRequestheader(),
      success: function (res, statusCode) {
        if (res.data.Success) {
          options.success.call(that, res.data.ResultData);
        } else {
          util.processFailureRequest(options.failure, res, statusCode);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
        
  },
  myGroupList: function (options) {
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/Group/myGroupList',
      method: 'POST',
      data: util.signature({
        user_id: options.user_id,
        pageNumber: options.pageIndex,
        pageSize: options.pageSize
      }),
      header: util.buildRequestheader(),
      success: function (res, statusCode) {
        if (res.data.Success) {
          options.success.call(that, res.data.ResultData);
        } else {
          util.processFailureRequest(options.failure, res, statusCode);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  groupDetail: function (options){
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/Group/getGroupDetail',
      method: 'POST',
      data: util.signature({
        group_id: options.group_id,
        user_id: options.user_id
      }),
      header: util.buildRequestheader(),
      success: function (res, statusCode) {
        if (res.data.Success) {
          options.success.call(that, res.data.ResultData);
        } else {
          util.processFailureRequest(options.failure, res, statusCode);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  userJoinGroupApply: function (options) {
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/Group/userJoinGroupApply',
      method: 'POST',
      data: util.signature({
        group_id: options.group_id,
        user_id: options.user_id,
      }),
      header: util.buildRequestheader(),
      success: function (res, statusCode) {
        if (res.data.Success) {
          options.success.call(that, res.data.ResultData);
        } else if (res.statusCode == 422) {
          var errors = res.data
          var msg = '';

          for (var errorcode in errors) {
            var errorItem = errors[errorcode];
            console.log(errorItem)
            for (var i = 0; i < errorItem.length; i++) {
              msg += errorItem[i] + '　';
            }
          }
          wx.showToast({
            title: msg,
            icon: 'loading',
            duration: 2000
          });
        }else {
          util.processFailureRequest(options.failure, res, statusCode);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  memberList: function (options){
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/Group/memberList',
      method: 'POST',
      data: util.signature({
        group_id: options.group_id,
        pageNumber: options.pageIndex,
        pageSize: options.pageSize
      }),
      header: util.buildRequestheader(),
      success: function (res, statusCode) {
        if (res.data.Success) {
          options.success.call(that, res.data.ResultData);
        } else {
          util.processFailureRequest(options.failure, res, statusCode);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  setMainGroup:function(options){
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/Group/setMainGroup',
      method: 'POST',
      data: util.signature({
        group_id: options.group_id,
        user_id: options.user_id,
      }),
      header: util.buildRequestheader(),
      success: function (res, statusCode) {
        if (res.data.Success) {
          options.success.call(that, res.data.ResultData);
        } else if (res.statusCode == 422) {
          var errors = res.data
          var msg = '';

          for (var errorcode in errors) {
            var errorItem = errors[errorcode];
            console.log(errorItem)
            for (var i = 0; i < errorItem.length; i++) {
              msg += errorItem[i] + '　';
            }
          }
          wx.showToast({
            title: msg,
            icon: 'loading',
            duration: 2000
          });
        } else {
          util.processFailureRequest(options.failure, res, statusCode);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  memberDetail:function(options){
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/Group/memberDetailInfo',
      method: 'POST',
      data: util.signature({
        user_id: options.user_id,
        group_id:options.group_id
      }),
      header: util.buildRequestheader(),
      success: function (res, statusCode) {
        if (res.data.Success) {
          options.success.call(that, res.data.ResultData);
        } else {
          util.processFailureRequest(options.failure, res, statusCode);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  removeMember: function (options){
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/Group/removeMember',
      method: 'POST',
      data: util.signature({
        group_id: options.group_id,
        user_id: options.user_id,
      }),
      header: util.buildRequestheader(),
      success: function (res, statusCode) {
        if (res.data.Success) {
          options.success.call(that, res.data.ResultData);
        } else if (res.statusCode == 422) {
          var errors = res.data
          var msg = '';

          for (var errorcode in errors) {
            var errorItem = errors[errorcode];
            console.log(errorItem)
            for (var i = 0; i < errorItem.length; i++) {
              msg += errorItem[i] + '　';
            }
          }
          wx.showToast({
            title: msg,
            icon: 'loading',
            duration: 2000
          });
        } else {
          util.processFailureRequest(options.failure, res, statusCode);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  GroupActivityListByStatus: function (options){
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/Group/GroupActivityListByStatus',
      method: 'POST',
      data: util.signature({
        user_id: options.user_id,
        status: options.status,
        pageSize: options.pageSize,
        pageNum: options.pageNum
      }),
      header: util.buildRequestheader(),
      success: function (res, statusCode) {
        if (res.data.Success) {
          options.success.call(that, res.data.ResultData);
        } else {
          util.processFailureRequest(options.failure, res, statusCode);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  joinMemberList: function (options){
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/Group/joinMemberList',
      method: 'POST',
      data: util.signature({
        activity_id: options.activity_id,
        pageSize: options.pageSize,
        pageNum: options.pageNum
      }),
      header: util.buildRequestheader(),
      success: function (res, statusCode) {
        if (res.data.Success) {
          options.success.call(that, res.data.ResultData);
        } else {
          util.processFailureRequest(options.failure, res, statusCode);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  },
  activityMemberDetail: function (options) {
    var util = require('../utils/util.js');
    var that = this;
    var app = getApp();
    wx.request({
      url: app.getApiPathPrefix() + '/Group/activityMemberDetail',
      method: 'POST',
      data: util.signature({
        user_id: options.user_id,
        activity_id: options.activity_id
      }),
      header: util.buildRequestheader(),
      success: function (res, statusCode) {
        if (res.data.Success) {
          options.success.call(that, res.data.ResultData);
        } else {
          util.processFailureRequest(options.failure, res, statusCode);
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器忙，请稍后重试',
          icon: 'loading',
          duration: 2000
        });
      }
    });
  }
}
module.exports = {
  getGroupList: userGroupService.getGroupList,
  isManager: userGroupService.isManager,
  myGroupList: userGroupService.myGroupList,
  groupDetail: userGroupService.groupDetail,
  userJoinGroupApply: userGroupService.userJoinGroupApply,
  memberList: userGroupService.memberList,
  setMainGroup: userGroupService.setMainGroup,
  memberDetail: userGroupService.memberDetail,
  removeMember: userGroupService.removeMember,
  GroupActivityListByStatus: userGroupService.GroupActivityListByStatus,
  joinMemberList: userGroupService.joinMemberList,
  activityMemberDetail: userGroupService.activityMemberDetail
}